package handle

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	if file.Size >= 1024*1024*1024*1 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "视频过大",
			"data": nil,
		})
		return
	}
	log.Println(file.Filename)
	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" && ext != ".flv" {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "视频格式不对",
			"data": nil,
		})
		return
	}
	dst := "D:\\gocode\\src\\video\\api\\upload\\video\\" + file.Filename
	// 上传文件至指定的完整文件路径
	//pkg.Upload(dst, file.Filename)
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "上传失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": file.Filename,
	})
}
